package com.game.timer;

import java.util.Date;
import org.alan.utils.TimeHelper;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.ScheduleBuilder;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SchedulerCenter {
    private SchedulerFactory schedulerFactory;

    private final Logger log = LoggerFactory.getLogger(getClass());

    public SchedulerCenter() {
        this.schedulerFactory = (SchedulerFactory)new StdSchedulerFactory();
    }

    public SchedulerCenter(String configFile) throws Exception {
        this.schedulerFactory = (SchedulerFactory)new StdSchedulerFactory(configFile);
    }

    public void addJob(SchedulerEvent schedulerEvent) throws Exception {
        String cronExpression = schedulerEvent.getCronExpression();
        JobDetail job = JobBuilder.newJob(JobDemo.class).build();
        job.getJobDataMap().put(SchedulerEvent.class.getSimpleName(), schedulerEvent);
        CronTrigger trigger = (CronTrigger)TriggerBuilder.newTrigger().withSchedule((ScheduleBuilder)CronScheduleBuilder.cronSchedule(cronExpression)).build();
        schedulerEvent.setJobKey(job.getKey());
        Scheduler scheduler = this.schedulerFactory.getScheduler();
        scheduler.scheduleJob(job, (Trigger)trigger);
        Date date = trigger.getNextFireTime();
        schedulerEvent.nextTime = date.getTime();
        this.log.info("加入新的计划任务"+ cronExpression + ",下次执行时间"+
                TimeHelper.getDate(date.getTime()));
        if (!scheduler.isStarted())
            scheduler.startDelayed(10);
    }

    public void removeJobs(SchedulerEvent schedulerEvent) {
        try {
            Scheduler scheduler = this.schedulerFactory.getScheduler();
            scheduler.deleteJob(schedulerEvent.getJobKey());
        } catch (SchedulerException e) {
            this.log.warn("", (Throwable)e);
        }
    }

    public static class JobDemo implements Job {
        public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
            JobDetail jobDetail = jobExecutionContext.getJobDetail();
            SchedulerEvent event = (SchedulerEvent)jobDetail.getJobDataMap().get(SchedulerEvent.class.getSimpleName());
            event.execute(jobExecutionContext);
            if (jobExecutionContext.getNextFireTime() == null) {
                event.nextTime = -1L;
            } else {
                event.nextTime = jobExecutionContext.getNextFireTime().getTime();
            }
        }
    }

    public static class ExecutorImlp implements Job {
        public void execute(JobExecutionContext jobExecutionContext) {
            System.out.println("--------" + hashCode() + "--------" +
                    System.currentTimeMillis() + "------------");
            try {
                Thread.sleep(10000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
